package com.cy.dda.service.customer.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.customer.CustomerAttachmentMapper;
import com.cy.dda.dao.mapper.customer.CustomerCompanyEditRecordMapper;
import com.cy.dda.dao.mapper.customer.CustomerCompanyMapper;
import com.cy.dda.dao.mapper.customer.CustomerContactMapper;
import com.cy.dda.dao.mapper.customer.CustomerInvestigationDescMapper;
import com.cy.dda.dao.mapper.customer.CustomerLiveAddressEditRecordMapper;
import com.cy.dda.dao.mapper.customer.CustomerMapper;
import com.cy.dda.dao.mapper.customer.CustomerMobileEditRecordMapper;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.ao.customer.ContactInfoEditAO;
import com.cy.dda.model.ao.customer.CustomerCompanyInfoEditAO;
import com.cy.dda.model.ao.customer.CustomerContactInfoEditAO;
import com.cy.dda.model.ao.customer.CustomerPersonalInfoEditAO;
import com.cy.dda.model.domain.customer.Customer;
import com.cy.dda.model.domain.customer.CustomerAttachment;
import com.cy.dda.model.domain.customer.CustomerCompany;
import com.cy.dda.model.domain.customer.CustomerCompanyEditRecord;
import com.cy.dda.model.domain.customer.CustomerContact;
import com.cy.dda.model.domain.customer.CustomerInvestigationDesc;
import com.cy.dda.model.domain.customer.CustomerLiveAddressEditRecord;
import com.cy.dda.model.domain.customer.CustomerMobileEditRecord;
import com.cy.dda.model.query.customer.CustomerListAdminQuery;
import com.cy.dda.model.query.customer.CustomerListQuery;
import com.cy.dda.model.vo.customer.CustomerAttachmentVO;
import com.cy.dda.model.vo.customer.CustomerCompanyInfoVO;
import com.cy.dda.model.vo.customer.CustomerContactInfoVO;
import com.cy.dda.model.vo.customer.CustomerDetailInfoAdminVO;
import com.cy.dda.model.vo.customer.CustomerDetailInfoVO;
import com.cy.dda.model.vo.customer.CustomerInvestigationDescVO;
import com.cy.dda.model.vo.customer.CustomerListAdminVO;
import com.cy.dda.model.vo.customer.CustomerListVO;
import com.cy.dda.model.vo.customer.CustomerPersonalDataVO;
import com.cy.dda.model.vo.customer.CustomerPersonalInfoVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.customer.CustomerService;

/**
 * 客户 CustomerService
 *
 * @author: tanyilin
 * @create: 2018年11月7日 下午3:53:35
 */
@Service
public class CustomerServiceImpl extends BaseService implements CustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private CustomerInvestigationDescMapper customerInvestigationDescMapper;
	@Autowired
	private CustomerContactMapper customerContactMapper;
	@Autowired
	private CustomerAttachmentMapper customerAttachmentMapper;
	@Autowired
	private CustomerMobileEditRecordMapper customerMobileEditRecordMapper;
	@Autowired
	private CustomerLiveAddressEditRecordMapper customerLiveAddressEditRecordMapper;
	@Autowired
	private CustomerCompanyEditRecordMapper customerCompanyEditRecordMapper;
	@Autowired
	private CustomerCompanyMapper customerCompanyMapper;
	

	@Override
	public PageBean<CustomerListAdminVO> selectCustomerListAdmin(CustomerListAdminQuery query) {
		int total = customerMapper.selectCustomerListAdminCount(query);
		List<CustomerListAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = customerMapper.selectCustomerListAdmin(query);
			list.stream().forEach(c -> {
				c.setRiskLevelText(c.getRiskLevel().getText());
			});
		}
		return new PageBean<CustomerListAdminVO>(list, total);
	}

	@Override
	public CustomerDetailInfoAdminVO selectCustomerDetailInfoAdminVO(Long customerId) {
		//查询客户信息
		CustomerDetailInfoAdminVO modelVo = customerMapper.selectCustomerDetailInfoAdminVO(customerId);
		state(modelVo != null, "客户信息不存在");

		//查询客户调查说明
		CustomerInvestigationDescVO investigationDescVo = customerInvestigationDescMapper.selectCustomerInvestigationDescVO(customerId);
		if(investigationDescVo != null) {
			BeanUtils.copyProperties(investigationDescVo, modelVo);
		}
		
		//查询客户联系人信息
		List<CustomerContactInfoVO> contactInfoList = customerContactMapper.selectCustomerContactInfoVO(customerId);
		if(!CommonUtils.isEmpty(contactInfoList)) {
			
			contactInfoList.stream().forEach(c -> {
				c.setContactTypeText(c.getContactType().getText());
			});
			modelVo.setContactInfoList(contactInfoList);
		}
		
		//查询客户附件资料信息
		List<CustomerAttachmentVO> attachmentList = customerAttachmentMapper.selectCustomerAttachmentVO(customerId);
		if(!CommonUtils.isEmpty(attachmentList)) {
			modelVo.setAttachmentList(attachmentList);
		}
		
		modelVo.setSexText(modelVo.getSex() == null ? "" : modelVo.getSex().getText());
		modelVo.setAge(CommonUtils.getAgeByIdNo(modelVo.getIdNo()));
		modelVo.setRiskLevelText(modelVo.getRiskLevel() == null ? "" : modelVo.getRiskLevel().getText());
		modelVo.setInvestigateUserName(CommonUtils.nameFormat(modelVo.getInvestigateUserName()));
		
		return modelVo;
	}

	@Override
	public List<CustomerMobileEditRecord> selectCustomerMobileEditRecord(Long customerId) {
		CustomerMobileEditRecord record = new CustomerMobileEditRecord();
		record.setCustomerId(customerId);
		List<CustomerMobileEditRecord> list = customerMobileEditRecordMapper.select(record);
		list.stream().forEach(c -> {
			c.setId(null);
			c.setCustomerId(null);
			c.setUserId(null);
		});
		return list;
	}

	@Override
	public List<CustomerLiveAddressEditRecord> selectCustomerLiveAddressEditRecord(Long customerId) {
		CustomerLiveAddressEditRecord record = new CustomerLiveAddressEditRecord();
		record.setCustomerId(customerId);
		List<CustomerLiveAddressEditRecord> list = customerLiveAddressEditRecordMapper.select(record);
		list.stream().forEach(c -> {
			c.setId(null);
			c.setCustomerId(null);
			c.setUserId(null);
		});
		return list;
	}

	@Override
	public List<CustomerCompanyEditRecord> selectCustomerCompanyEditRecord(Long customerId) {
		CustomerCompanyEditRecord record = new CustomerCompanyEditRecord();
		record.setCustomerId(customerId);
		List<CustomerCompanyEditRecord> list = customerCompanyEditRecordMapper.select(record);
		list.stream().forEach(c -> {
			c.setId(null);
			c.setCustomerId(null);
			c.setUserId(null);
		});
		return list;
	}

	@Override
	@Transactional
	public void addAttachment(CustomerAttachment customerAttachment) {
		
		Date time = new Date();
		customerAttachment.setCreateTime(time);
		customerAttachment.setUpdateTime(time);
		customerAttachmentMapper.insertSelective(customerAttachment);
	}

	@Override
	public CustomerPersonalDataVO selectCustomerPersonalDataVO(Long customerId) {
		CustomerPersonalDataVO customerPersonalDataVO = customerMapper.selectCustomerPersonalDataVO(customerId);
		state(customerPersonalDataVO != null, "客户信息不存在");
		
		//查询客户联系人信息
		List<CustomerContactInfoVO> contactInfoList = customerContactMapper.selectCustomerContactInfoVO(customerId);
		if(!CommonUtils.isEmpty(contactInfoList)) {
			
			contactInfoList.stream().forEach(c -> {
				c.setContactTypeText(c.getContactType().getText());
			});
			customerPersonalDataVO.setContactInfoList(contactInfoList);
		}
		
		return customerPersonalDataVO;
	}

	@Override
	public PageBean<CustomerListVO> selectCustomerList(CustomerListQuery query) {
		int total = customerMapper.selectCustomerListCount(query);
		List<CustomerListVO> list = new ArrayList<CustomerListVO>();
		if(total > 0) {
			query.startPage();
			list = customerMapper.selectCustomerList(query);
			list.stream().forEach(c -> {
				c.setRiskLevelText(c.getRiskLevel().getText());
			});
		}
		
		return new PageBean<CustomerListVO>(list, total);
	}

	@Override
	public CustomerDetailInfoVO selectCustomerDetailInfoVO(Long customerId) {
		//查询客户信息
		CustomerDetailInfoAdminVO modelVo = customerMapper.selectCustomerDetailInfoAdminVO(customerId);
		state(modelVo != null, "客户信息不存在");
		
		CustomerDetailInfoVO customerDetailInfoVO = new CustomerDetailInfoVO();
		BeanUtils.copyProperties(modelVo, customerDetailInfoVO);
		
		//查询客户调查说明
		CustomerInvestigationDescVO investigationDescVo = customerInvestigationDescMapper.selectCustomerInvestigationDescVO(customerId);
		if(investigationDescVo != null) {
			BeanUtils.copyProperties(investigationDescVo, customerDetailInfoVO);
		}
		
		customerDetailInfoVO.setSexText(modelVo.getSex() == null ? "" : modelVo.getSex().getText());
		customerDetailInfoVO.setAge(CommonUtils.getAgeByIdNo(modelVo.getIdNo()));
		customerDetailInfoVO.setRiskLevelText(modelVo.getRiskLevel() == null ? "" : modelVo.getRiskLevel().getText());
		customerDetailInfoVO.setInvestigateUserName(CommonUtils.nameFormat(customerDetailInfoVO.getInvestigateUserName()));
		
		return customerDetailInfoVO;
	}

	@Override
	public List<CustomerAttachmentVO> selectCustomerAttachmentList(Long customerId) {
		//查询客户附件资料信息
		List<CustomerAttachmentVO> attachmentList = customerAttachmentMapper.selectCustomerAttachmentVO(customerId);
		return attachmentList;
	}

	@Override
	public CustomerPersonalInfoVO selectCustomerPersonalInfo(Long customerId) {
		
		CustomerPersonalDataVO customerPersonalDataVO = customerMapper.selectCustomerPersonalDataVO(customerId);
		state(customerPersonalDataVO != null, "客户信息不存在");
		
		CustomerPersonalInfoVO customerPersonalInfoVO = new CustomerPersonalInfoVO();
		BeanUtils.copyProperties(customerPersonalDataVO, customerPersonalInfoVO);
		
		return customerPersonalInfoVO;
	}

	@Override
	public List<CustomerContactInfoVO> selectCustomerContactInfo(Long customerId) {
		//查询客户联系人信息
		List<CustomerContactInfoVO> contactInfoList = customerContactMapper.selectCustomerContactInfoVO(customerId);
		contactInfoList.stream().forEach(c -> {
			c.setContactTypeText(c.getContactType().getText());
		});
		return contactInfoList;
	}

	@Override
	public CustomerCompanyInfoVO selectCustomerCompanyInfoVO(Long customerId) {

		CustomerPersonalDataVO customerPersonalDataVO = customerMapper.selectCustomerPersonalDataVO(customerId);
		state(customerPersonalDataVO != null, "客户信息不存在");
		
		CustomerCompanyInfoVO customerCompanyInfoVO = new CustomerCompanyInfoVO();
		BeanUtils.copyProperties(customerPersonalDataVO, customerCompanyInfoVO);
		
		return customerCompanyInfoVO;
	}

	@Override
	@Transactional
	public void updateCustomerPersonalInfo(CustomerPersonalInfoEditAO editAO, Long userId) {
		Date time = new Date();
		Long customerId = editAO.getCustomerId();
		Customer dbCustomer = customerMapper.selectByPrimaryKey(customerId);
		state(dbCustomer != null, "客户不存在");
		
		Customer updateCustomer = new Customer();
		BeanUtils.copyProperties(editAO, updateCustomer);
		updateCustomer.setId(customerId);
		updateCustomer.setUpdateTime(time);
		customerMapper.updateByPrimaryKeySelective(updateCustomer);
		
		if(!CommonUtils.isEmpty(updateCustomer.getMobile())) {
			//如果联系方式发生变更,则记录
			if(!updateCustomer.getMobile().equals(dbCustomer.getMobile())) {
				CustomerMobileEditRecord mobileEditRecord = new CustomerMobileEditRecord();
				mobileEditRecord.setCustomerId(customerId);
				mobileEditRecord.setMobile(updateCustomer.getMobile());
				mobileEditRecord.setUserId(userId);
				mobileEditRecord.setCreateTime(time);
				customerMobileEditRecordMapper.insertSelective(mobileEditRecord);
			}
		}
		
		if(!CommonUtils.isEmpty(updateCustomer.getLiveAddress())) {
			//如果居住地址发生变更,则记录
			if(!updateCustomer.getLiveAddress().equals(dbCustomer.getLiveAddress())) {
				CustomerLiveAddressEditRecord liveAddressEditRecord = new CustomerLiveAddressEditRecord();
				liveAddressEditRecord.setCustomerId(customerId);
				liveAddressEditRecord.setLiveAddress(updateCustomer.getLiveAddress());
				liveAddressEditRecord.setUserId(userId);
				liveAddressEditRecord.setCreateTime(time);
				customerLiveAddressEditRecordMapper.insertSelective(liveAddressEditRecord);
			}
		}
		
	}

	@Override
	@Transactional
	public void updateCustomerContactInfo(CustomerContactInfoEditAO editAO) {
		
		Date time = new Date();
		Long customerId = editAO.getCustomerId();
		//得到最新的客户联系人信息列表
		List<ContactInfoEditAO> contactInfoList = editAO.getCustomerContactList();
		List<CustomerContact> customerContactList = new ArrayList<>();
		if(!CommonUtils.isEmpty(contactInfoList)) {
			for(ContactInfoEditAO contactInfoEditAO : contactInfoList) {
				CustomerContact customerContact = new CustomerContact();
				BeanUtils.copyProperties(contactInfoEditAO, customerContact);
				customerContact.setCustomerId(customerId);
				customerContact.setCreateTime(time);
				customerContact.setUpdateTime(time);
				customerContactList.add(customerContact);
			}
			//批量新增
			customerContactMapper.insertBatch(customerContactList);
			//删除无效数据
			customerContactMapper.deleteByUpdateTime(customerId, time);
		}
	}

	@Override
	@Transactional
	public void updateCustomerCompanyInfo(CustomerCompanyInfoEditAO editAO, Long userId) {

		Date time = new Date();
		Long customerId = editAO.getCustomerId();
		CustomerCompany dbCustomerCompany = new CustomerCompany();
		dbCustomerCompany.setCustomerId(customerId);
		dbCustomerCompany = customerCompanyMapper.selectOne(dbCustomerCompany);
		state(dbCustomerCompany != null, "客户单位信息异常");
		
		//更新单位信息
		CustomerCompany updateCustomerCompany = new CustomerCompany();
		BeanUtils.copyProperties(editAO, updateCustomerCompany);
		updateCustomerCompany.setId(dbCustomerCompany.getId());
		updateCustomerCompany.setUpdateTime(time);
		customerCompanyMapper.updateByPrimaryKeySelective(updateCustomerCompany);
		
		//添加单位信息更新记录
		CustomerCompanyEditRecord companyEditRecord = new CustomerCompanyEditRecord();
		BeanUtils.copyProperties(editAO, companyEditRecord);
		companyEditRecord.setUserId(userId);
		companyEditRecord.setCreateTime(time);
		customerCompanyEditRecordMapper.insertSelective(companyEditRecord);
	}

	@Override
	@Transactional
	public void addCustomerInvestigationDesc(CustomerInvestigationDesc customerInvestigationDesc) {
		customerInvestigationDescMapper.insertSelective(customerInvestigationDesc);
	}
	
	
}
